﻿using System;
using DAL;
using Entity;
using NLog; 
public partial class Admin_AddCategory : ParentPage
{
    private static Logger _logger = LogManager.GetCurrentClassLogger();

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                if (Request.Url.AbsoluteUri.IndexOf("CategoryID=") >= 0)
                {
                    ManageCategory objDALManageCategory = new ManageCategory();
                    Category objCategory = objDALManageCategory.GetCategoryInfo(Request.QueryString["CategoryID"].ToString());
                    txtCategoryName.Text = objCategory.CategoryName;
                    ddlCategoryStatus.SelectedValue = objCategory.CategoryStatus.ToString();
                }
            }
        }
        catch (Exception ex)
        {
            _logger.Error("Admin_AddCategory:::PageLoad:::" + ex.Message);
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            Category objCategory = new Category();
            ManageCategory objManageCategory = new ManageCategory();
            objCategory.CategoryName = txtCategoryName.Text;
            objCategory.CategoryStatus = Convert.ToInt16(ddlCategoryStatus.SelectedValue);
            if (Request.Url.AbsoluteUri.IndexOf("CategoryID=") >= 0)
            {
                objCategory.CategoryID = Convert.ToInt16(Request.QueryString["CategoryID"].ToString());
                int nReturnValue = objManageCategory.EditGategoryInfo(objCategory);
                if (nReturnValue == 1)
                {
                    tr_UserMessage.InnerHtml = "Edit Category Successfully";
                    Response.Redirect("Category", false);
                }
                else
                {
                    tr_UserMessage.InnerHtml = "you have issues please contact the system support";
                }
            }
            else
            {
                int nReturnValue = objManageCategory.AddNewCategory(objCategory);
                if (nReturnValue == 1)
                {
                    tr_UserMessage.InnerHtml = "Add Category Successfully";
                    Response.Redirect("Category", false);
                }
                else
                {
                    tr_UserMessage.InnerHtml = "you have issues please contact the system support";
                }
            }
        }
        catch (Exception ex)
        {
            _logger.Error("Admin_AddCategory:::btnSubmit_Click:::" + ex.Message);
        }
    }
}